const app = getApp()
const {
  GiftShopModel
} = require('../../../utils/giftshop-model.js')
const giftShopModel = new GiftShopModel()
const {
  MemberModel
} = require('../../../utils/member-model.js')
const memberModel = new MemberModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modules: [],
    isLogin: false,
    loginShow: false,
    lotteryTotal: 0,
    integralTotal: 0,
    defaultCurrent: 0,
    currentBannerIndex: 0,
  },

  currentBannerChange(e) {
    this.setData({
      currentBannerIndex: e.detail.current
    })
  },
  clickBannerItem(e) {
    wx.reportAnalytics('gift_home_banner')
    const options = e.currentTarget.dataset.options || {}
    const shopInfo = wx.getStorageSync('shopInfo')
    if (options.jumpType == 1) {
      if (options.jumpUrl) {
        let goUrl = `${options.jumpUrl}?shopId=${shopInfo.shopId}`
        wx.navigateTo({
          url: goUrl
        })
      }
    } else if (options.jumpType == 2) {
      wx.navigateToMiniProgram({
        appId: options.jumpAppId,
        path: options.jumpUrl || 'pages/index/index',
        extraData: {},
        envVersion: 'release',
        success(res) {}
      })
    } else if (options.jumpType == 3) {
      if (options.jumpUrl) {
        wx.navigateTo({
          url: `/pages/h5/index?url=${encodeURIComponent(`${options.jumpUrl}`)}`,
        })
      }
    }
  },
  jumpShop(e) {
    const { item } = e.currentTarget.dataset
    if (item.redrect_type === 4) {
      wx.reportAnalytics('gift_home_goods_classify')
      wx.navigateTo({
        url: `/pages/giftShop/goodsclassify/index?navigateCode=${item.navigate_code}`
      })
    } else if (item.redrect_type === 3) {
      // 商品品牌
    } else if (item.redrect_type === 2) {
      wx.reportAnalytics('gift_home_custom_link')
      wx.navigateTo({
        url: item.link
      })
    }
  },
  clickMore(e) {
    wx.reportAnalytics('gift_home_more')
    const { detail } = e.currentTarget.dataset
    wx.navigateTo({
      url: detail.content_more_link
    })
  },
  goGoodsDetail(e) {
    const { goods } = e.currentTarget.dataset
    wx.reportAnalytics('gift_home_goods_detail');
    wx.navigateTo({
      url: `/pages/giftShop/goodsDetail/index?shopId=${goods.shopId}&skuId=${goods.skuId}&channelGoodsId=${goods.channelGoodsId}`
    })
  },

  getModules() {
    const shopInfo = wx.getStorageSync('shopInfo')
    const merchantId = wx.getStorageSync('merchantId')
    giftShopModel.getHomeModule({
      merchantId,
      shopId: shopInfo.shopId,
      brandId: shopInfo.brandId
    }).then(data => {
      this.setData({
        currentBannerIndex: 0,
        defaultCurrent: 0,
        modules: data || []
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none'
      })
    })
  },

  getMemberInfo() {
    const merchantId = wx.getStorageSync('merchantId')
    const shopInfo = wx.getStorageSync('shopInfo')
    this.setData({
      isLogin: true
    })
    giftShopModel.userAssetShop({
      merchantId,
      shopId: shopInfo.shopId
    }).then(data => {
      this.setData({
        lotteryTotal: data.lotteryTotal || 0,
        integralTotal: data.integralTotal || 0
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none'
      })
    })
  },

  showLogin() {
    this.setData({
      loginShow: true
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      isLogin: app.globalData.isLogin
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getModules()
    if (app.globalData.isLogin) {
      this.getMemberInfo()
    }
  }
})